package com.exam.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.exam.bean.PaperSourceType;
import com.exam.service.PaperSourceTypeService;
import com.exam.service.impl.PaperSourceTypeServiceImpl;
import com.exam.util.Page;
import com.exam.util.PageResult;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 来源分类Action 2013-1-3
 * 
 * @author MuLing
 *
 */
public class PaperSourceTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private PaperSourceType paperSourceType = new PaperSourceType();
	private List<PaperSourceType> pstList = new ArrayList<PaperSourceType>();
	private Page page = new Page();

	// 为分页处理服务的方法
	@SuppressWarnings("unchecked")
	public void listForSourceTypePageService(PaperSourceTypeService pcts,
			String strWhere) throws Exception { 
		PageResult pageResult = null;
		getPage().setEveryPage(10);
		if (strWhere != null && !"".equals(strWhere)) {
			pageResult = pcts.findListByPage(strWhere, getPage());// 按条件查询
		} else {
			pageResult = pcts.findListByPage(getPage());// 简单分页
		}
		setPage(pageResult.getPage());// 获得分页信息
		setPstList(pageResult.getList());// 获得记录
	}

	//按条件进行分页查询
	public String listByWherePage(){
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String nameStr = request.getParameter("name"); 
		try {
			if(nameStr!=null&&!"".equals(nameStr)){
				if(page.getCurrentPage()>1){//存在条件的时候，将分页信息设置为初始态
					page = new Page();
				}
				listForSourceTypePageService(service, "name='"+nameStr+"'");
			}else{
				listForSourceTypePageService(service, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}
	
	//使用按分页查询
	public String listByPage() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		try {
			listForSourceTypePageService(service, "");
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}
	 
	//添加来源类型
	public String add() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		try {
			service.add(getPaperSourceType());
			listForSourceTypePageService(service,"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}
	
	//编辑来源类型
	public String edit() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String strID = request.getParameter("id");
		if (strID != null) {
			try {
				setPaperSourceType(service.findModel(Integer.parseInt(strID)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "exception";
			}
		}
		return "edit";
	}
	
	//更新来源类型
	public String update() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		try {
			service.update(getPaperSourceType());
			listForSourceTypePageService(service,"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}
	
	//删除来源类型
	public String delete() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String strID = request.getParameter("id");
		if (strID != null) {
			try {
				service.delete(Integer.parseInt(strID));
				listForSourceTypePageService(service,"");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "exception";
			}
		}
		return "listByPage";
	}

	//批量删除分类信息
		public String deleteList(){
			PaperSourceTypeService service = new PaperSourceTypeServiceImpl(); 
			HttpServletRequest request = ServletActionContext.getRequest();// 取得Request对象
			String[] checkIDs = request.getParameterValues("checkIDs");// 取得页面上需要删除的信息对应的ID
			
			List<Integer> idList = new ArrayList<Integer>();// 用于存储需要删除的信息对应的ID的集合
			if (checkIDs != null && checkIDs.length > 0) {
				for (String strID : checkIDs) {
					idList.add(Integer.parseInt(strID));// 将ID保存到List集合中
				}
			} 
			try {
				service.delete(idList);
				listForSourceTypePageService(service, "");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "exception";
			} 
			return "listByPage";
		}
	
	public List<PaperSourceType> getPstList() {
		return pstList;
	}

	public void setPstList(List<PaperSourceType> pstList) {
		this.pstList = pstList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public PaperSourceType getPaperSourceType() {
		return paperSourceType;
	}

	public void setPaperSourceType(PaperSourceType paperSourceType) {
		this.paperSourceType = paperSourceType;
	}
 
}
